package com.itheima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.DishFlavor;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Override
    public void saveDish(DishDto dishDto) {
        //讲数据存入dish表内
        dishMapper.insert(dishDto);
        //获取id
        Long id = dishDto.getId();

        //从dto表中获取从前端发送来的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //判断集合是否有数据
        if (flavors != null && flavors.size() > 0) {
            //遍历集合获取每一条口味数据
            for (DishFlavor flavor : flavors) {
                //口味表一行数据中  菜品与之关联的dish-id，从dish表获取id并添加到dishflavor中
                flavor.setDishId(id);
                dishFlavorMapper.insert(flavor);
            }
        }

    }
}
